package flexUnitTests
{
	import cn.fengsweb.tokyotyrant.TokyoTyrantQuery;
	import cn.fengsweb.tokyotyrant.TokyoTyrantResponder;
	import cn.fengsweb.tokyotyrant.TokyoTyrantTable;
	import cn.fengsweb.tokyotyrant.define.TokyoTyrantQueryConditionType;
	import cn.fengsweb.tokyotyrant.define.TokyoTyrantSetIndexType;
	
	import flexunit.framework.TestCase;
	
	public class Test_TokyoTyrantTable extends TestCase
	{
		private var _keyValueArray:Array;
		
		public function Test_TokyoTyrantTable(methodName:String=null)
		{
			super(methodName);
		}
		
		override public function setUp():void
		{
			TokyoTyrantTable.host	= "10.34.11.62"
			TokyoTyrantTable.port	= 1972;
			
			super.setUp();
		}
		
		override public function tearDown():void
		{
			super.tearDown();
		}
		
		private function initSomeData():void
		{
			_keyValueArray	= new Array();
			_keyValueArray['key1']	= {
				hello:"world"
			};
			_keyValueArray['key2']	= {
				hello:"world",
				author:"fengsweb"
			};
			TokyoTyrantTable.putMore(_keyValueArray, new TokyoTyrantResponder(result, status));
			
			function result(obj:Array):void{
				for(var key:* in obj){
					assertTrue(obj[key]);
				}
			}
			function status(type:uint):void{
				fail("Status: "+type);
			}
		}
		
		public function testGetMore():void
		{
			initSomeData();
			
			TokyoTyrantTable.getMore(["key1","key2"], new TokyoTyrantResponder(result, status));
			
			function result(obj:Array):void{
				for each(var item:Object in obj){
					assertObjectEquals(_keyValueArray[item['key']], item['value']);
				}
			}
			function status(type:uint):void{
				fail("Status: "+type);
			}
		}
		
		public function testGetOne():void
		{
			initSomeData();
			
			TokyoTyrantTable.getOne("key1", new TokyoTyrantResponder(result));
			
			function result(obj:*):void{
				assertObjectEquals(_keyValueArray["key1"], obj);
			}
			function status(type:uint):void{
				fail("Status: "+type);
			}
		}
		
		public function testOutMore():void
		{
			initSomeData();
			
			TokyoTyrantTable.outMore(["key1","key2"], new TokyoTyrantResponder(result, status));
			
			function result(obj:*):void{
				for(var key:* in obj){
					assertTrue(obj[key]);
				}
			}
			function status(type:uint):void{
				fail("Status: "+type);
			}
		}
		
		public function testOutOne():void
		{
			initSomeData();
			
			TokyoTyrantTable.outOne("key1", new TokyoTyrantResponder(result, status));
			
			function result(obj:*):void{
				assertTrue(obj);
			}
			function status(type:uint):void{
				fail("Status: "+type);
			}
		}
		
		public function testPutMoreNoKey():void
		{
			var valueArray:Array	= new Array();
				valueArray.push({key:"value"});
				valueArray.push({hello:"world",world:"hello"});
			
			TokyoTyrantTable.putMoreNoKey(valueArray, new TokyoTyrantResponder(result, status));
			
			function result(obj:Array):void{
				assertEquals(2, obj.length);
			}
			function status(type:uint):void{
				fail("Status: "+type);
			}
		}
		
		public function testPutOneNoKey():void
		{
			TokyoTyrantTable.putOneNoKey({key:"value"}, new TokyoTyrantResponder(result, status));
			
			function result(obj:Number):void{
				assertTrue(obj>0);
			}
			function status(type:uint):void{
				fail("Status: "+type);
			}
		}
		
		public function testPutMore():void
		{
			initSomeData();
		}
		
		public function testPutOne():void
		{
			TokyoTyrantTable.putOne("key6",{key:"value"}, new TokyoTyrantResponder(result, status));
			
			function result(obj:*):void{
				assertTrue(obj);
			}
			function status(type:uint):void{
				fail("Status: "+type);
			}
		}
		
		public function testVanish():void
		{
			TokyoTyrantTable.vanish(new TokyoTyrantResponder(result, status));
			
			function result(obj:*):void{
				assertTrue(obj);
			}
			function status(type:uint):void{
				fail("Status: "+type);
			}
		}
		
		public function testQuery():void
		{
			TokyoTyrantTable.vanish(null);
			initSomeData();
			
			var ttq:TokyoTyrantQuery	= new TokyoTyrantQuery();
				ttq.addCondition("hello", "world", TokyoTyrantQueryConditionType.STROR);
			
			TokyoTyrantTable.query(ttq, new TokyoTyrantResponder(result, status));
			
			function result(obj:Array):void{
				for each(var item:Object in obj){
					assertObjectEquals(_keyValueArray[item['key']], item['value']);
				}
			}
			function status(type:uint):void{
				fail("Status: "+type);
			}
		}
		
		public function testQueryLimit():void
		{
			TokyoTyrantTable.vanish(null);
			initSomeData();
			
			var ttq:TokyoTyrantQuery	= new TokyoTyrantQuery();
				ttq.addCondition("hello", "world");
				ttq.setLimit(1,1);
			
			TokyoTyrantTable.query(ttq, new TokyoTyrantResponder(result, status));
			
			function result(obj:Array):void{
				for each(var item:Object in obj){
					assertObjectEquals(_keyValueArray["key2"], item['value']);
				}
			}
			function status(type:uint):void{
				fail("Status: "+type);
			}
		}
		
		public function testQueryCount():void
		{
			TokyoTyrantTable.vanish(null);
			initSomeData();
			
			var ttq:TokyoTyrantQuery	= new TokyoTyrantQuery();
				ttq.addCondition("hello", "world");
				ttq.count();
			
			TokyoTyrantTable.query(ttq, new TokyoTyrantResponder(result, status));
			
			function result(obj:*):void{
				for(var key:String in obj) trace(key+": "+obj[key]);
				assertEquals(2, obj)
			}
			function status(type:uint):void{
				fail("Status: "+type);
			}
		}
		
		public function testStat():void
		{
			TokyoTyrantTable.stat(new TokyoTyrantResponder(result));
			
			function result(obj:*):void{
				assertEquals("Linux", obj['os']);
			}
			function status(type:uint):void{
				fail("Status: "+type);
			}
		}
		
		public function testSetIndex():void
		{
			TokyoTyrantTable.setIndex("id", TokyoTyrantSetIndexType.LEXICAL, new TokyoTyrantResponder(result, status));
			
			function result(obj:*):void{
				assertTrue(obj);
			}
			function status(type:uint):void{
				fail("Status: "+type);
			}
		}
	}
}